package com.kk.netty.nio.network.selector;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

import static com.kk.netty.nio.ByteBufferUtil.debugAll;

@Slf4j
public class MultiThreadServer {
    public static void main(String[] args)  throws IOException{
        Thread.currentThread ().setName ("boss");
        ServerSocketChannel ssc = ServerSocketChannel.open ( );
        ssc.configureBlocking (false);
        Selector boos = Selector.open ( );
        SelectionKey boosKey = ssc.register (boos, 0, null);
        boosKey.interestOps (SelectionKey.OP_ACCEPT);
        ssc.bind (new InetSocketAddress (8080));
        // 1、创建固定数量的 worker 并初始化
        // Runtime.getRuntime ().availableProcessors () 处理器数量
        Worker[] workers = new Worker[Runtime.getRuntime ().availableProcessors ()];
        for (int i = 0; i < workers.length; i++) {
            workers[i] = new Worker ("WORKER-"+i);
        }
        AtomicInteger index = new AtomicInteger ();
        while (true) {
            boos.select ();
            Iterator<SelectionKey> iter = boos.selectedKeys ( ).iterator ( );
            while (iter.hasNext ( )) {
                SelectionKey key = iter.next ( );
                iter.remove ();
                if (key.isAcceptable ( )) {
                    SocketChannel sc = ssc.accept ( );
                    sc.configureBlocking (false);
                    log.debug("connected...{}", sc.getRemoteAddress());
                    // 2. 关联 selector
                    log.debug("before register...{}", sc.getRemoteAddress());
                    // 轮训
                    workers[index.getAndIncrement ()%workers.length].register (sc);
                    log.debug("after register...{}", sc.getRemoteAddress());
                }
            }
        }
    }

    static class Worker implements Runnable {
        private Thread thread;
        private Selector selector;
        private String name;
        private volatile boolean start = false;// 还未初始化
        private ConcurrentLinkedQueue<Runnable> queue = new ConcurrentLinkedQueue<> ( );

        private Worker(String name) {
            this.name = name;
        }

        // 初始化线程 和 selector
        public void register(SocketChannel sc) throws IOException {
            if (!start) {
                selector = Selector.open ( );
                thread = new Thread (this, name);
                thread.start ( );
                start = true;
            }
            // 唤醒 select 方法 boos
            selector.wakeup ( );
            // boos
            sc.register (selector, SelectionKey.OP_READ, null);
        }

        @Override
        public void run() {
            while (true) {
                try {
                    selector.select ( );// worker-0  阻塞
                    Iterator<SelectionKey> iter = selector.selectedKeys ( ).iterator ( );
                    while (iter.hasNext ( )) {
                        SelectionKey key = iter.next ( );
                        iter.remove ( );
                        if (key.isReadable ( )) {
                            ByteBuffer buffer = ByteBuffer.allocate (16);
                            SocketChannel channel = (SocketChannel) key.channel ( );
                            log.debug ("read...{}", channel.getRemoteAddress ( ));
                            channel.read (buffer);
                            buffer.flip ( );
                            debugAll (buffer);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace ( );
                }
            }
        }
    }
}
